<template>
	<view :class="$attrs" class="bg-white radius-8 mx-16 px-16 py-12 mb-12" style="box-shadow:0px 0px 6px 0px rgba(0, 0, 0, 0.05);">
		<view class="flex justify-between item-center mb-12">
			<text class="text-color-regular fs-16 text-bold">营销功能</text>
		</view>
		<view style="display:grid;grid-template-columns:repeat(4, minmax(0, 1fr));gap:12px 0;">
			<template v-for="(menu,i) in menus" :key="i">
				<view @click="onMenuClick(menu)" v-if="menu.show!=false">
					<uni-badge v-if="i===3 && merCount" class="flex center" is-dot :text="merCount" absolute="rightTop" :offset="[10, 0]">
						<view class="flex-col center">
							<image class="mb-4 w-24 h-24" :src="menu.icon"></image>
							<text class="text-color-hint fs-12">{{menu.title}}</text>
						</view>
					</uni-badge>
					<view class="flex-col center" v-else>
						<image class="mb-4 w-24 h-24" :src="menu.icon"></image>
						<text class="text-color-hint fs-12">{{menu.title}}</text>
					</view>
				</view>
			</template>
		</view>
	</view>
</template>

<script setup>
	import { computed, ref, onMounted } from 'vue'
	import {
		useUserStore
	} from '@/stores/modules/user.js'
	import { WX_AppID_Merchant } from '@/config/index.js'
	import { waitAuditCount } from '@/api/index.js'

	const userStore = useUserStore()
	const merCount = ref(0)

	const menus = computed(() => [
		{
			title: '订单管理',
			icon: '../../../static/svg/mine/menu-order.svg',
			path: '/pages/home/home?tab=1'
		},
		{
			show: userStore.isMerchant,
			title: '我的活动',
			icon: '../../../static/svg/mine/menu-activity.svg',
			path: '/pagesB/my-activity/my-activity?from=mine'
		},
		{
			show: userStore.isMerchant,
			title: '商户管理',
			icon: '../../../static/svg/mine/menu-merchant-manage.svg',
			path: '/pagesD/merchant/merchant'
		},
		{
			title: '角色管理',
			icon: '../../../static/svg/mine/menu-role.svg',
			path: '/pagesD/role/role',
			show:userStore.agentState == 2||userStore.businessInfo,
		},
		{
			title: '火火学院',
			icon: '../../../static/svg/mine/menu-college.svg',
			path: '/pagesA/college/college'
		},
		{
			show: userStore.isMerchant,
			title: '商户工作台',
			icon: '../../../static/svg/mine/menu-merchant-work.svg',
			appID: WX_AppID_Merchant,
			path: ''
		},
		{
			show: userStore.isMerchant,
			title: '红包中心',
			icon: '../../../static/svg/mine/menu-red-packet.svg',
			path: '/pagesA/redpacket/redpacket'
		},
		{
			show: false,//userStore.isMerchant,
			title: '发票管理',
			icon: '../../../static/svg/mine/menu-invoice.svg',
			path: ''
		},
		{
			show: userStore.isMerchant,
			title: '积分商城',
			icon: '../../../static/svg/mine/menu-integral.svg',
			integral: true
		}
	])

	const onMenuClick = (menu) => {
		// 小程序跳转
		if (menu && menu.appID) {
			uni.navigateToMiniProgram({
				appId: menu.appID,
				path: menu.path,
				envVersion: 'trial', // 仅在当前小程序为开发版或体验版时此参数有效。如果当前小程序是正式版，则打开的小程序必定是正式版。
				extraData: {
					// TODO 自动登录参数，待开发
				}
			})
			return
		}
		if (menu && menu.integral) {
			if(userStore.userInfo.businessInfo.pointsMallState === 0){
				uni.navigateTo({
					url: '/pagesF/index/index'
				})
			}else{
				uni.navigateTo({
					url: '/pagesJ/integralSetting/mall'
				})
			}
		}
		if (menu && menu.path) {
			uni.navigateTo({
				url: menu.path
			})
		}
	}
	onMounted(() => {
		waitAuditCount().then(res => {
			merCount.value = res
		})
	})
</script>
